package com.wjggy.core.action.posts;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.wjggy.core.action.view.PostsView;
import com.wjggy.core.common.json.AjaxJsonUtils;
import com.wjggy.core.common.page.Page;
import com.wjggy.core.common.page.PageUtil;
import com.wjggy.core.common.springview.ViewUtil;
import com.wjggy.core.common.util.DateUtil;
import com.wjggy.core.common.util.FckUtil;
import com.wjggy.core.entities.Category;
import com.wjggy.core.entities.Posts;
import com.wjggy.core.entities.User;
import com.wjggy.core.service.posts.CategoryService;
import com.wjggy.core.service.posts.ForumService;
import com.wjggy.core.service.posts.PostsService;


 /**
 *  <p>Description:  文章编辑action</p>
 *  <p>Copyright:Copyright (c) 2012  </p>
 *  <p>Company:wjggy  </p>
 *  @author xiaofan.yu  
 *  @date   2012-2-3 下午05:48:06    
 *  @project  wjggy
 *  @version  1.0.0
 *  @see   
 *  @since 1.6 
 */
@Controller
@RequestMapping("/admin/post")
public class PostsAction {
	
	@Resource(name="postsService")
	PostsService postsService;
	
	@Resource(name="categoryService")
	CategoryService categoryService;
	@Resource(name="forumService")
	ForumService forumService;
	
	
	@RequestMapping(value="")
	public ModelAndView postList(Posts posts,HttpServletRequest request, HttpServletResponse response){
		 ModelAndView model=ViewUtil.findView(PostsView.POSTS_URL);
		 Page page= PageUtil.createPage(request,"findPosts.htm","forumsid");
		 page.setResult(postsService.findPosts(posts,page));
		 model.addObject("page", page);
		 model.addObject("posts", posts);
		 model.addObject("categorys", categoryService.findCategory(null));
		 model.addObject("forums",forumService.findForums(null));
		return  model;
	}
	/**
	 *  @describe  文章添加页面
	 *  @param request
	 *  @param response
	 *  @return
	 */
	@RequestMapping(value="add", method=RequestMethod.GET)
	public ModelAndView addPost(HttpServletRequest request, HttpServletResponse response) {
		 ModelAndView model=ViewUtil.findView(PostsView.POSTS_ADD_URL);
		 model.addObject("categorys", categoryService.findCategory(null));
		 model.addObject("forums",forumService.findForums(null));
		 model.addObject("fck", FckUtil.createFCK(request, "postcontent","请输入文章内容"));
		return  model;
	}
	
	/**
	 *  @describe 文章插入
	 *  @param posts
	 *  @param request
	 *  @param response
	 *  @return
	 */
	@RequestMapping(value="add",method=RequestMethod.POST)
	public ModelAndView insertPosts(Posts posts,HttpServletRequest request, HttpServletResponse response) {
		 User user=(User) request.getAttribute("user");
		 posts.setPostauthor(user.getId());
		 posts.setUpdatedate(DateUtil.date2Str2());
		 posts.setPostdate(DateUtil.date2Str2());
		 Category category=categoryService.getCategory(forumService.getForums(posts.getForumsid()).getCategoryid());
		 posts.setCategoryid(category.getCategoryid());
		 posts.setCategoryName(category.getName());
		 posts.setAuthorName(user.getUsernicename());
		 //设置新增时间作者及修改时间
		 postsService.addPosts(posts);
		return  postList(new Posts(),request,response);
	}
	
	/**
	 *  @describe 文章修改
	 *  @param posts
	 *  @param request
	 *  @param response
	 *  @return
	 */
	@RequestMapping(value="update",method=RequestMethod.POST)
	public ModelAndView updatePosts(Posts posts,HttpServletRequest request, HttpServletResponse response) {
		User user=(User) request.getAttribute("user");
		posts.setPostauthor(user.getId());
		posts.setUpdatedate(DateUtil.date2Str2()) ;
		Category category=categoryService.getCategory(forumService.getForums(posts.getForumsid()).getCategoryid());
		posts.setCategoryid(category.getCategoryid());
		posts.setCategoryName(category.getName());
		posts.setAuthorName(user.getUsernicename());
		if(postsService.updatePosts(posts)){
			return postList(posts,request,response);
		}else {
			return updatePostInfo(posts.getPostsid(), request, response);
		}
	}
	
	/**
	 *  @describe 文章编辑页面
	 *  @param posts
	 *  @param request
	 *  @param response
	 *  @return
	 */
	@RequestMapping(value="update/{id}",method=RequestMethod.GET)
	public ModelAndView updatePostInfo(@PathVariable Long id,HttpServletRequest request, HttpServletResponse response) {
		 ModelAndView model=ViewUtil.findView(PostsView.POSTS_UPDATE_URL);
		 Posts posts=new Posts();
		 posts.setPostsid(id);
		 Posts post=postsService.findPost(posts);
		 model.addObject("posts", post);
		 model.addObject("categorys", categoryService.findCategory(null));
		 model.addObject("forums",forumService.findForums(null));
		 model.addObject("fck", FckUtil.createFCK(request, "postcontent",post.getPostcontent()));
		 return  model;
	}
	
	/**
	 *  @describe 查找文章列表
	 *  @param posts
	 *  @param request
	 *  @param response
	 *  @return
	 */
	@RequestMapping("/findPosts")
	public ModelAndView findPosts(Posts posts,HttpServletRequest request, HttpServletResponse response) {
		 ModelAndView model=ViewUtil.findView(PostsView.POSTS_URL);
		 System.out.println("forumsid:"+request.getParameter("forumsid"));;
		 Page page= PageUtil.createPage(request,"findPosts.htm","forumsid");
		 page.setResult(postsService.findPosts(posts,page));
		 model.addObject("page", page);
		 model.addObject("categorys", categoryService.findCategory(null));
		 model.addObject("forums",forumService.findForums(null));
		 model.addObject("fck", FckUtil.createFCK(request, "postcontent",posts.getPostcontent()));
		return  model;
	}
	/**
	 *  @describe 查找文章列表
	 *  @param posts
	 *  @param request
	 *  @param response
	 *  @return
	 * @throws IOException 
	 */
	@RequestMapping(value="delete/{id}")
	public String deletePosts(@PathVariable Long id,HttpServletRequest request, HttpServletResponse response) throws IOException {
		Posts posts=new Posts();
		posts.setPostsid(id);
		postsService.deletePosts(posts);
//		Map map=new HashMap();
//		map.put("status", true);
//		map.put("message", "删除成功");
//		AjaxJsonUtils.toJson(map, request, response);
//		return null;
		return "redirect:/admin/post";
	}
}

